package org.aman.mongo.util;

import java.util.Date;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoOptions;
import com.mongodb.ServerAddress;

public class MongoFactory {
    private static Mongo tweetMongo;

    /** 互推广告 */
    public static final String spread_app = "spread_app";
    
    /** 互推广告 */
    public static final String spread_app_imei = "spread_app_imei";

    /** 推送安装数据 */
    public static final String push_data = "push_data";

    /** 推送统计 */
    public static final String channel_data_statistics = "channel_data_statistics";

    /** 被推送统计 */
    public static final String spread_app_data_statistics = "spread_app_data_statistics";
    
    public static String server;
    public static int port;

    /**
     * 初始化
     */
    static {
        try {

            MongoOptions mongoOptions = new MongoOptions();
            mongoOptions.autoConnectRetry = false;
            mongoOptions.connectionsPerHost = 1000;
            mongoOptions.maxWaitTime = 1;
            mongoOptions.connectTimeout = 5000;

            tweetMongo = new Mongo(new ServerAddress(server, port),
                    mongoOptions);

        } catch (Exception e) {
        }
    }

    /**
     * 广告数据库
     * 
     * @return
     */
    public static DBCollection getTweetMongo() {
        try {
            DB db = tweetMongo.getDB("tweet");
            DBCollection collection = db.getCollection("spread_app");
            return collection;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 
     * @Author daijiangguo 2014-6-16 上午9:45:26
     * @param collection
     * @return
     */
    public static DBCollection getTweetMongo(String collection) {
        try {
            DB db = tweetMongo.getDB("tweet");
            DBCollection col = db.getCollection(collection);
            return col;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 
     * @Author daijiangguo 2014-6-16 上午9:45:31
     * @param db
     * @param collection
     * @return
     */
    public static DBCollection getTweetMongoYmd(String db, String collection) {
        try {
            DB d = tweetMongo.getDB(db);
            DBCollection col = d.getCollection(collection);
            return col;
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * 
     * @Author daijiangguo 2014-6-16 上午9:45:40
     * @param db
     * @param collection
     * @return
     */
    public static DBCollection getTweetMongo(String db, String collection) {
        try {
            DB d = tweetMongo.getDB(db);
            DBCollection col = d.getCollection(collection);
            return col;
        } catch (Exception e) {
        }
        return null;
    }
}
